package cn.edu.zjicm.wordsnet_d.data.test.selection;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.widget.TextView;

import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.data.RelationShip;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.util.LogUtil;
import cn.edu.zjicm.wordsnet_d.util.StringUtil;

public class Cloze{
	protected List<Integer> used = new LinkedList<Integer>();
	WordFactory wf;
	Context mContext;
	String promopt = null;
	OneWord theWord;
	
	String where, where0, where1, where2;
	int total = -1;
	
	public Cloze(Context mContext){
		this.mContext = mContext;
		this.wf = WordFactory.getInstance(mContext);;
	}
	
	public String getQuestion(RelationShip r) {
		// TODO Auto-generated method stub
		used.clear();
		used.add(r.getIndex_word_id());

		String colloc_eng = r.getColloc_eng();
		String colloc_cn = r.getColloc_cn();
		promopt=colloc_eng + colloc_cn;
		
		theWord = wf.getOneWordById(r.getIndex_word_id());
		
		init();
		
		int pos[] = RelationShip.getWordPosiont(r);
		return StringUtil.hideWord(mContext, colloc_eng, pos, theWord.getLemma());
	}
	
	public String getRightChoice(RelationShip r) {
		// TODO Auto-generated method stub
		return theWord.getLemma();
	}

	public void promopt(TextView tv) {
		// TODO Auto-generated method stub
		tv.setText(promopt);
	}
	
	void init(){
		where2 = "index_word_id!=" + theWord.getIndex_word_id() + " and lemma_type =" + theWord.getLemma_type();
		where1 = "degree_fm=" + theWord.getDegree_fm() + " and " + where2;
		where0 = "sense_degree_fm>0 and sense_degree_fm<1000 and " + where1;
		
		//由于选择题错误答案的生成较慢，减少搜索条件，只保留where2
		where = where0;		
		total = wf.getInt("select count(1) from words inner join words_log using (index_word_id) where " + where);
		if (total < 5) {
			where = where1;
			total = wf.getInt("select count(1) from words inner join words_log using (index_word_id) where " + where);
			if (total < 5) {
				where = where2;
				total = wf.getInt("select count(1) from words inner join words_log using (index_word_id) where " + where);
			}
		}
	}
	
	//暂时不管词性
	/*
	void init(){
		where2 = "Index_word_id!=" + theWord.getIndex_word_id();
		where1 = "degree_fm=" + OneWord.DEGREE_UNFAMILIAR + " and " + where2;
		where0 = "sense_degree_fm>0 and " + where1;
		where = "senses_senior like '%" + WordFactory.getType(theWord.getSensesSenior()) + "%'" + " and " + where0;
		
		total = wf.getInt("select count(1) from words where " + where);
		if (total < 5) {
			where = where0;
			total = wf.getInt("select count(1) from words where " + where);
			if (total < 5) {
				where = where1;
				total = wf.getInt("select count(1) from words where " + where);
				if(total < 5) {
					where = where2;
					total = wf.getInt("select count(1) from words where " + where);		
				}
			}
		}
	}
	*/
	
	public OneWord getWrongChoice(RelationShip r) {
		// TODO Auto-generated method stub

		//Log.e("type", theWord.getLemma());
		//Log.e("type", theWord.getSensesSenior());
		//Log.e("type", WordFactory.getType(theWord.getSensesSenior()));

		OneWord wrong = null;
		if(total > 0) {
			for (int i = 0; i < 10 // 似乎有重复记录，避免死循环
					        && (wrong == null || used.contains(wrong.getIndex_word_id())); i++) {
				wrong = wf.getOneWordOfWhere1(where 
						                     //+ " order by degree_fm"
						                     + " limit " + WordFactory.rand(total) + ",1");
//				LogUtil.e("获取错误选项", i + "");
			}
		}
		if(total == 0 || wrong == null || used.contains(wrong.getIndex_word_id())){
			while(wrong == null || used.contains(wrong.getIndex_word_id())) {
				wrong = wf.getOneWordOfWhere(where2 + " limit " + WordFactory.rand(50) + ",1");
			}
		}
		used.add(wrong.getIndex_word_id());
		return wrong;
	}
}
